package com.tensquare.search.service;

import com.tensquare.search.dao.SearchDao;
import com.tensquare.search.pojo.Article;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import util.IdWorker;

@Service
public class SearchService {

    @Autowired
    private SearchDao searchDao;
    @Autowired
    private IdWorker idWorker;

    //根据标题或内容进行文章的搜索
    public PageResult<Article> findByTitleAndContentLike(int page, int size, String  keywords) {
        //索引键有两个 需要传两次
        Page<Article> byTitleOrContentLike = searchDao.findByTitleOrContentLike(keywords,keywords, PageRequest.of(page - 1, size));
        return new PageResult<>(byTitleOrContentLike.getTotalElements(),byTitleOrContentLike.getContent());
    }
    //添加文章
    public void addArticle(Article article) {
        System.out.println("article = " + article);
        article.setId(idWorker.nextId()+"");
        System.out.println("article = " + article);
        searchDao.save(article);
    }
}
